package demo.spring.securing.manager;

import demo.spring.securing.SecurityUser;
import demo.spring.securing.User;
import demo.spring.securing.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * Created by lushun.chen on 2017/4/16.
 *
 * @author lushun.chen
 */
@Component
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user = userRepository.findByName(s);
        if (user == null) {
            throw new UsernameNotFoundException("UserName " + s + " not found");
        }

        return new SecurityUser(user);
    }
}
